import 'package:flutter/material.dart';
import 'package:flutter_widgets_pc/widgets/button/kq_ink_well.dart';

import 'package:get/get.dart';

import '../../config/kq_global.dart';
import '../../resources/images.dart';
import '../../resources/kq_images.dart';
import '../../resources/kq_theme_colors.dart';
import '../../theme/kq_theme_manager.dart';
import '../divider/kq_divider.dart';
import '../expandableText/kq_expandable_text.dart';

/// 详情专用View，仅用于查看，不能编辑
///
/// @author 周卓
///
/// 组成部分：【左边必填星号】+【左边标题】+ 【左边自定义View】 + 【右边内容】+ 【右边自定义View】+【右边箭头】
class KqDetailsItemView extends StatefulWidget {
  /// 左侧Text
  final String title;

  /// 右侧Text
  final String content;

  /// 标题和文字的字体大小，默认16
  final double? fontSize;

  /// 是否必填
  final bool showMustInput;

  /// 是否显示右侧Text的下划线
  final bool showUnderline;

  /// 是否多行显示，默认true
  final bool multiLine;

  /// 最大行数
  final int? maxLines;

  /// 对齐方式，默认右对齐
  final TextAlign? textAlign;

  /// 是否隐藏右侧文字
  final bool hideRightText;

  /// 是否显示右侧箭头
  final bool showRightArrow;

  /// 是否隐藏底部分割线
  final bool hideDivider;

  /// 垂直对齐方式，默认居上对齐
  final CrossAxisAlignment crossAxisAlignment;

  /// 整个Item点击
  final VoidCallback? onTaped;

  final Widget? customLeftView;

  /// 自定义右侧的View
  final Widget? customRightView;

  /// 是否启用内容折叠，默认开启
  final bool expandContentEnable;

  /// 右侧文字点击回调，考虑下划线文字可点击
  final Function(String text)? onRightTextTaped;

  /// 自定义箭头颜色，不过需要注意的是，现在的图标是有透明度的。
  final Color? arrowIconColor;

  /// 背景颜色设置
  final Color? bgColor;

  const KqDetailsItemView({
    Key? key,
    required this.title,
    this.content = '',
    this.fontSize,
    this.showMustInput = false,
    this.showUnderline = false,
    this.hideRightText = false,
    this.showRightArrow = false,
    this.hideDivider = false,
    this.multiLine = true,
    this.textAlign = TextAlign.end,
    this.crossAxisAlignment = CrossAxisAlignment.start,
    this.maxLines,
    this.expandContentEnable = true,
    this.onTaped,
    this.arrowIconColor,
    this.bgColor,
    this.onRightTextTaped,
    this.customLeftView,
    this.customRightView,
  }) : super(key: key);

  @override
  KqDetailsItemViewState createState() => KqDetailsItemViewState();
}

class KqDetailsItemViewState extends State<KqDetailsItemView> {
  /// 画笔，用于测量
  TextPainter painter = TextPainter(textDirection: TextDirection.ltr);

  /// 获取1行高度
  double measureTextHeight(String text, {TextStyle? textStyle}) {
    painter.text = TextSpan(
      text: text,
      style: textStyle,
    );

    painter.layout();
    return painter.height;
  }

  @override
  Widget build(BuildContext context) {
    var titleTextStyle = TextStyle(
        color: KqThemeManager.getCommonConfig().itemTitleColor ??
            KqThemeColors.text26,
        fontSize: widget.fontSize ??
            KqThemeManager.instance.getConfig().formConfig.titleFontSize ??
            16);
    // 一行标题的高度
    double titleOneLineHeight =
        measureTextHeight(widget.title, textStyle: titleTextStyle);

    return Column(
      children: [
        KqInkWell(
          onTap: widget.onTaped,
          backgroundColor: widget.bgColor ??
              KqThemeManager.getCommonConfig().itemBgColor ??
              KqThemeColors.bgWhite,
          child: Container(
            width: context.width,
            padding: EdgeInsets.only(
              right: KqThemeManager.instance
                      .getConfig()
                      .formConfig
                      .paddingHorizontal ??
                  16,
            ),
            child: Row(
              crossAxisAlignment: widget.crossAxisAlignment,
              children: [
                Container(
                  alignment: Alignment.centerRight,
                  width: KqThemeManager.instance
                          .getConfig()
                          .formConfig
                          .paddingHorizontal ??
                      16,
                  height: titleOneLineHeight,
                  margin: EdgeInsets.only(
                      top: KqThemeManager.instance
                              .getConfig()
                              .formConfig
                              .paddingVertical ??
                          15),
                  padding: const EdgeInsets.only(
                    right: 2,
                  ),
                  child: Text(
                    '*',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        color: widget.showMustInput
                            ? KqThemeColors.textRed
                            : KqThemeColors.bgTransparent,
                        fontSize: KqThemeManager.instance
                                .getConfig()
                                .formConfig
                                .mustInputFontSize ??
                            14),
                  ),
                ),
                Padding(
                  padding: EdgeInsets.symmetric(
                      vertical: KqThemeManager.instance
                              .getConfig()
                              .formConfig
                              .paddingVertical ??
                          15),
                  child: Text(
                    widget.title,
                    style: titleTextStyle,
                  ),
                ),
                SizedBox.fromSize(size: const Size(4, 1)),
                widget.customLeftView ?? Container(),
                SizedBox.fromSize(size: const Size(6, 1)),
                widget.hideRightText
                    ? Container()
                    : Expanded(
                        child: InkWell(
                        onTap: widget.onRightTextTaped == null
                            ? null
                            : () =>
                                widget.onRightTextTaped?.call(widget.content),
                        child: Padding(
                          padding: EdgeInsets.symmetric(
                              vertical: KqThemeManager.instance
                                      .getConfig()
                                      .formConfig
                                      .paddingVertical ??
                                  15),
                          child: widget.expandContentEnable
                              ? KqExpandableText(
                                  text: widget.content,
                                  maxLines: widget.maxLines ?? 5,
                                  textAlign: widget.textAlign ?? TextAlign.end,
                                  textStyle: TextStyle(
                                      color: KqThemeManager.getCommonConfig()
                                              .itemContentColor ??
                                          KqThemeColors.text59,
                                      fontSize: widget.fontSize ??
                                          KqThemeManager.instance
                                              .getConfig()
                                              .formConfig
                                              .contentFontSize ??
                                          16,
                                      decoration: widget.showUnderline
                                          ? TextDecoration.underline
                                          : TextDecoration.none),
                                )
                              : Text(
                                  widget.content,
                                  textAlign: widget.textAlign ?? TextAlign.end,
                                  maxLines:
                                      widget.multiLine ? widget.maxLines : 1,
                                  overflow: widget.multiLine &&
                                          widget.maxLines == null
                                      ? null
                                      : TextOverflow.ellipsis,
                                  style: TextStyle(
                                      color: KqThemeManager.getCommonConfig()
                                              .itemContentColor ??
                                          KqThemeColors.text59,
                                      fontSize: widget.fontSize ??
                                          KqThemeManager.instance
                                              .getConfig()
                                              .formConfig
                                              .contentFontSize ??
                                          16,
                                      decoration: widget.showUnderline
                                          ? TextDecoration.underline
                                          : TextDecoration.none),
                                ),
                        ),
                      )),
                widget.customRightView ?? Container(),
                Visibility(
                    visible: widget.showRightArrow,
                    child: Container(
                        padding: const EdgeInsets.only(left: 4),
                        margin: EdgeInsets.only(
                            top: widget.crossAxisAlignment ==
                                    CrossAxisAlignment.start
                                ? (KqThemeManager.instance
                                        .getConfig()
                                        .formConfig
                                        .paddingVertical ??
                                    15)
                                : 0),
                        width: titleOneLineHeight,
                        height: titleOneLineHeight,
                        alignment: Alignment.center,
                        child: Image.asset(
                          Images.commonIcArrowRight,
                          width: titleOneLineHeight,
                          height: titleOneLineHeight,
                          color: widget.arrowIconColor ?? KqThemeColors.text8C,
                          package: KqGlobal.packageName,
                        ))),
              ],
            ),
          ),
        ),

        /// 分割线
        Visibility(visible: !widget.hideDivider, child: KqDivider())
      ],
    );
  }
}
